<?php

namespace App\Models\Api;

use App\Models\Common\CollOption;

class SearchEngine extends ApiModel
{
    //
    protected $table = 'search_engine';

    protected $guarded = [];

    protected $hidden = [
        'uid', 'jobsort1', 'jobsort2', 'jobsort3', 'jobsort4',
        'jobsort5', 'jobarea1', 'jobarea2', 'jobarea3', 'jobarea4', 'jobarea5',
        'industry1', 'industry2', 'industry3', 'industry4', 'industry5', 'created_at',
    ];

    /**
     * The attributes that should be casted to native types.
     * 转换属性 把int属性转为string类型
     * @var array
     */
    protected $casts = [
        'id' => 'string',
        'uid' => 'string',
        'age' => 'string',
    ];

    /**
     * 获取职位id数组
     *
     * @return array
     */
    public function getJobsortAttribute()
    {
        $arr = [];

        //遍历地区
        for ($i = 1; $i <= 5; $i++) {
            $value = $this['jobsort' . $i];
            if ($value != 0) {
                $arr[] = $value;
            }
        }
        return $arr;
    }

    /**
     * 获取职位名
     *
     * @return string
     */
    public function getJobsortNameAttribute()
    {
        return implode(',', \DB::table('coll_position')
            ->whereIn('cid', $this['jobsort'])->pluck('name'));
    }

    /**
     * 获取地区id数组
     *
     * @return array
     */
    public function getJobareaAttribute()
    {
        $arr = [];

        //遍历地区
        for ($i = 1; $i <= 5; $i++) {
            $value = $this['jobarea' . $i];
            if ($value != 0) {
                $arr[] = $value;
            }
        }
        return $arr;
    }

    /**
     * 获取地区名
     *
     * @return string
     */
    public function getJobareaNameAttribute()
    {
        $area = [];
        $town = [];
        foreach ($this->jobarea as $item) {
            if (strlen(strval($item)) > 6) {
                $town[] = $item;
            } else {
                $area[] = $item;
            }
        }
        $areaName = [];
        if (count($area) > 0) {
            $areaName = CollArea::whereIn('cid', $area)->pluck('name', 'cid')->toArray();
        }
        $townName = [];
        if (count($town) > 0) {
            $townName = CollTown::whereIn('cid', $town)->pluck('name', 'cid')->toArray();
        }

        $arr = $areaName + $townName;
        $str = '';
        //遍历地区
        foreach ($this->jobarea as $item) {
            if ($item != 0 && array_key_exists($item, $arr)) {
                $str .= $arr[$item] . ',';
            }
        }
        return rtrim($str, ',');
    }

    /**
     * 获取地区id数组
     *
     * @return array
     */
    public function getIndustryAttribute()
    {
        $arr = [];

        //遍历地区
        for ($i = 1; $i <= 5; $i++) {
            $value = $this['industry' . $i];
            if ($value != 0) {
                $arr[] = $value;
            }
        }
        return $arr;
    }

    /**
     * 获取地区名
     *
     * @return string
     */
    public function getIndustryNameAttribute()
    {

        $ids = $this->industry;

        $arr = [];
        foreach ($ids as $id) {
            $arr[] = CollOption::getOptionName($id, 'industry');
        }
        return implode(',', $arr);
    }

    /**
     * 获取学历名
     *
     * @return string
     */
    public function getEducationNameAttribute()
    {
        $eduId = $this->attributes['education'];
        if ($eduId == 0) {
            return '学历不限';
        }
        return CollOption::getOptionName($eduId, 'education');
    }

    /**
     * 获取工作经验名
     *
     * @return string
     */
    public function getWorkYearNameAttribute()
    {
        $workId = $this->attributes['work_year'];
        if ($workId == 0) {
            return '经验不限';
        }
        return CollOption::getOptionName($workId, 'work_year');
    }

    public function getReleaseDateNameAttribute()
    {
        $id = $this->attributes['release_date'];
        if ($id == 0) {
            return '';
        }
        return CollOption::getOptionName($id, 'release_date');
    }
}
